package com.youyi.flymewallpager.wallpager;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.service.wallpaper.WallpaperService;
import android.view.SurfaceHolder;

import com.youyi.flymewallpager.drawable.StarDrawable;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xzp on 16/6/22.
 */
public class SimpleWallPager extends WallpaperService{
    public static final String TAG = "SimpleWallPager";

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public Engine onCreateEngine() {
        return new StarEngine();
    }
    private class StarEngine extends Engine{
        private boolean visible=true;
        private List<Drawable> starList;
        private Handler mHandler;
        private Runnable mDrawRunnable=new Runnable() {
            @Override
            public void run() {
                draw();
            }
        };
//        private Bitmap mBitmap;
//        private Canvas mCanvas;
        public StarEngine() {
            super();
            SharedPreferences sp=getBaseContext().getSharedPreferences("simple-settings", Context.MODE_PRIVATE);
            int number=sp.getInt("number",1);
            String name=sp.getString("mode","StarDrawable");
            Class drawableClass;
            try {
                drawableClass=Class.forName(getPackageName()+".drawable."+name);
            } catch (ClassNotFoundException e) {
                drawableClass=StarDrawable.class;
            }
            starList=new ArrayList<Drawable>(number);

            for(int i=0;i<number;i++){
                try {
                    starList.add((Drawable) drawableClass.newInstance());
                } catch (Exception e) {
                    starList.add(new StarDrawable());
                }
            }
            mHandler=new Handler();
            //handler.post(mDrawRunnable);
        }

        @Override
        public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
//            mBitmap=Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888);
//            mCanvas=new Canvas(mBitmap);
            super.onSurfaceChanged(holder, format, width, height);
        }

        @Override
        public void onVisibilityChanged(boolean visible) {
            this.visible=visible;
            mHandler.removeCallbacks(mDrawRunnable);
            if(visible){
                mHandler.post(mDrawRunnable);
            }
            super.onVisibilityChanged(visible);
        }

        @Override
        public void onSurfaceCreated(SurfaceHolder holder) {
            super.onSurfaceCreated(holder);
            visible=true;
            mHandler.post(mDrawRunnable);
        }

        @Override
        public void onSurfaceDestroyed(SurfaceHolder holder) {
            super.onSurfaceDestroyed(holder);
            visible=false;
            mHandler.removeCallbacks(mDrawRunnable);
        }

        private void draw(){
            SurfaceHolder holder = getSurfaceHolder();
            Canvas originCanvas = null;
            try {
                originCanvas=holder.lockCanvas();
                if(originCanvas!=null){
//                    mBitmap.eraseColor(Color.rgb(0,0,0));
                    originCanvas.drawColor(Color.BLACK);
                    int starCount=starList.size();
                    for(int i=0;i<starCount;i++){
                        Drawable star=starList.get(i);
                        star.draw(originCanvas);
                    }
//                    originCanvas.drawBitmap(mBitmap,0,0,null);
                }
            }finally {
                if (originCanvas != null)
                    holder.unlockCanvasAndPost(originCanvas);
            }
            mHandler.removeCallbacks(mDrawRunnable);
            if(visible)
                mHandler.postDelayed(mDrawRunnable,40);
        }
    }
}
